package wcs

import (
	"encoding/json"
	"gitee.com/wcs2/common/models"
)

// OFS子任务-输送机任务表
type BizSubTaskTl struct {
	models.BaseModel
	TaskCode     string      `gorm:"type:varchar(260);column:task_code;unique" json:"taskCode"`       //comment:任务编码
	ParentCode   string      `gorm:"type:varchar(60);column:parent_code;" json:"parentCode"`          //comment:父任务编码
	TaskStatus   string      `gorm:"not null;type:varchar(60);column:task_status" json:"taskStatus"`  //comment:当前任务状态
	Step         string      `gorm:"type:varchar(60);column:step;" json:"step"`                       //comment:任务节点
	ExtData      models.JSON `gorm:"type:json;column:ext_data" json:"extData"`                        //comment:扩展信息
	TaskType     string      `gorm:"not null;type:varchar(60);column:task_type;" json:"taskType"`     //comment:任务类型
	FinishedTime models.Time `gorm:"column:finished_time;" json:"finishedTime"`                       //comment:任务完成时间
	MpCode       string      `gorm:"column:mp_code;type:varchar(60);" json:"mpCode"`                  //comment:机台编码
	ActionType   string      `gorm:"not null;type:varchar(60);column:action_type;" json:"actionType"` //comment:动作类型（SNED-送箱，RECEIVE-接箱）
	Position     string      `gorm:"type:varchar(60);column:position" json:"position"`                //comment:位置
	TlTaskCode   string      `gorm:"type:varchar(60);column:tl_task_code" json:"tlTaskCode"`          //comment:报文序号
	AreaCode     string      `gorm:"type:varchar(60);column:area_code" json:"areaCode"`               //comment:区域编码
	BoxNum       int         `gorm:"column:box_num" json:"boxNum"`                                    //comment:箱子数量
	TrySendTimes int         `gorm:"column:try_send_times" json:"trySendTimes"`                       //comment:重发次数
	Md5          string      `gorm:"type:varchar(200);column:md5;unique" json:"md5"`                  //comment:concat(parent_code, tl_task_code, ext_data)取Md5
}

func (bizSubTaskTl *BizSubTaskTl) TableName() string {
	return "biz_sub_task_tl"
}

func (bizSubTaskTl *BizSubTaskTl) AddExtData(key string, value interface{}) {
	var extDataMap map[string]interface{}
	if err := json.Unmarshal(bizSubTaskTl.ExtData, &extDataMap); err != nil {
		extDataMap = make(map[string]interface{})
	}
	extDataMap[key] = value
	if extData, err := json.Marshal(extDataMap); err == nil {
		bizSubTaskTl.ExtData = extData
	}
}

// 向任务表的扩展字体中添加Map数据。
func (bizSubTaskTl *BizSubTaskTl) AddExtDataMap(value map[string]interface{}) {
	var extDataMap map[string]interface{}
	if err := json.Unmarshal(bizSubTaskTl.ExtData, &extDataMap); err != nil {
		extDataMap = make(map[string]interface{})
	}
	for key, val := range value {
		extDataMap[key] = val
	}
	if extData, err := json.Marshal(extDataMap); err == nil {
		bizSubTaskTl.ExtData = extData
	}
}

// 将任务表中的扩展字体转换为Map
func (bizSubTaskTl *BizSubTaskTl) GetExtDataMap() map[string]interface{} {
	var extDataMap map[string]interface{}
	if err := json.Unmarshal(bizSubTaskTl.ExtData, &extDataMap); err == nil {
		return extDataMap
	} else {
		extDataMap = make(map[string]interface{})
		return extDataMap
	}
}
